package leetcode;

public class _1026 {
    /**
     *节点与其祖先之间的最大差值
     */
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    class Solution {
        public int maxAncestorDiff(TreeNode root) {
            return dfs(root,root.val,root.val);
        }
        public int dfs(TreeNode root,int min,int max){
            if(root==null){
                return 0;
            }
            min  = Math.min(min,root.val);
            max = Math.max(max,root.val);
            int ans = Math.max(Math.abs(min-root.val),Math.abs(max-root.val));
            ans = Math.max(ans,dfs(root.left,min,max));
            ans = Math.max(ans,dfs(root.right,min,max));
            return ans;
        }
    }
}
